package com.example.demo.mapimage;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
 
import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
 
public class DealWithImage {
 
	/**
	 * 图像处理: 1.截图 2.横向拼接 3.纵向拼接
	 * 
	 * @param path
	 *            获取的地图的位置
	 * @param count
	 *            最后合成的图片的信息 int[0]行数 int[1]每行的图片数
	 * @throws Exception
	 */
	public static void DealWithPhotos(String path, int[] count) throws Exception {
		File f = new File(path + "\\getMaps");
		String[] SourceImageList = f.list();
		/**
		 * 对列表内的图片进行切割 每行第一张图片截取整张图(获取图中的所有像素),本行的其他图片截取右半部分
		 */
		System.out.println("开始对图片进行第一次截取...");
		for (int i = 1, j = 0; i <= SourceImageList.length; i++) {
			if (i == (j * count[1] + 1)) {
				cutImage(path + "\\getMaps\\" + i + ".png", path + "\\cuttedMaps\\" + i + ".png", 0, 0, 2048, 2048);
				j++;
			} else {
				cutImage(path + "\\getMaps\\" + i + ".png", path + "\\cuttedMaps\\" + i + ".png", 2048 - 932, 0, 932,
						2048);
			}
		}
 
		/**
		 * 对切割后小图片进行拼接
		 */
		System.out.println("开始对图片进行第一次拼接...");
		ArrayList<String> arrImgs = new ArrayList<String>();
		for (int i = 1, j = 1; i <= SourceImageList.length; i++) {
			if (i != (j * count[1])) {
				arrImgs.add(path + "\\cuttedMaps\\" + i + ".png");
			} else {
				arrImgs.add(path + "\\cuttedMaps\\" + i + ".png");
				String[] imgs = (String[]) arrImgs.toArray(new String[arrImgs.size()]);
				mergeImage(imgs, 1, path + "\\mergedMaps\\" + j + ".png");
				arrImgs.clear();
				j++;
			}
		}
 
		/**
		 * 对生成的长图进行截图 第一行截取 x:0 y:0 h:1141 中间行截取 x:0 y:0 h:1140 尾行全截取
		 */
		System.out.println("开始对图片进行第二次截取...");
		File f1 = new File(path + "\\mergedMaps");
		String[] mergedImgs = f1.list();
		for (int i = 1; i <= mergedImgs.length; i++) {
			if (i != mergedImgs.length) {
				cutImage(path + "\\mergedMaps\\" + i + ".png", path + "\\cutteddMaps\\" + i + ".png", 0, 0,
						(count[1] - 1) * 932 + 2048, 1140);
			} else {
				cutImage(path + "\\mergedMaps\\" + i + ".png", path + "\\cutteddMaps\\" + i + ".png", 0, 0,
						(count[1] - 1) * 932 + 2048, 2048);
			}
		}
 
		/**
		 * 对第二次切割后的长图进行拼接
		 */
		System.out.println("开始对图片进行第二次拼接...");
		File f9 = new File(path + "\\cutteddMaps");
		ArrayList<String> cutteddList = new ArrayList<String>();
		for (int i = 1; i <= f9.list().length; i++) {
			cutteddList.add(path + "\\cutteddMaps\\" + i + ".png");
		}
		String[] cuttedd = (String[]) cutteddList.toArray(new String[cutteddList.size()]);
		mergeImage(cuttedd, 2, path + "\\map.png");
 
	}
 
	/**
	 * @Description:截图
	 * @author:liuyc
	 * @time:2016年5月27日 上午10:18:23
	 * @param srcFile源图片、targetFile截好后图片全名、startAcross
	 *            开始截取位置横坐标、StartEndlong开始截图位置纵坐标、width截取的长，hight截取的高
	 */
	public static void cutImage(String srcFile, String targetFile, int startAcross, int StartEndlong, int width,
			int hight) throws Exception {
		// 取得图片读入器
		Iterator<ImageReader> readers = ImageIO.getImageReadersByFormatName("png");
		ImageReader reader = readers.next();
		// 取得图片读入流
		InputStream source = new FileInputStream(srcFile);
		ImageInputStream iis = ImageIO.createImageInputStream(source);
		reader.setInput(iis, true);
		// 图片参数对象
		ImageReadParam param = reader.getDefaultReadParam();
		Rectangle rect = new Rectangle(startAcross, StartEndlong, width, hight);
		param.setSourceRegion(rect);
		BufferedImage bi = reader.read(0, param);
		ImageIO.write(bi, targetFile.split("\\.")[1], new File(targetFile));
	}
 
	/**
	 * @Description:图片拼接 （注意：必须两张图片长宽一致哦）
	 * @author:liuyc
	 * @time:2016年5月27日 下午5:52:24
	 * @param files
	 *            要拼接的文件列表
	 * @param type
	 *            1横向拼接， 2 纵向拼接
	 */
	public static void mergeImage(String[] files, int type, String targetFile) {
		int len = files.length;
		if (len < 1) {
			throw new RuntimeException("图片数量小于1");
		}
		File[] src = new File[len];
		BufferedImage[] images = new BufferedImage[len];
		int[][] ImageArrays = new int[len][];
		for (int i = 0; i < len; i++) {
			try {
				src[i] = new File(files[i]);
				images[i] = ImageIO.read(src[i]);
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
			int width = images[i].getWidth();
			int height = images[i].getHeight();
			ImageArrays[i] = new int[width * height];
			ImageArrays[i] = images[i].getRGB(0, 0, width, height, ImageArrays[i], 0, width);
		}
		int newHeight = 0;
		int newWidth = 0;
		for (int i = 0; i < images.length; i++) {
			// 横向
			if (type == 1) {
				newHeight = newHeight > images[i].getHeight() ? newHeight : images[i].getHeight();
				newWidth += images[i].getWidth();
			} else if (type == 2) {// 纵向
				newWidth = newWidth > images[i].getWidth() ? newWidth : images[i].getWidth();
				newHeight += images[i].getHeight();
			}
		}
		if (type == 1 && newWidth < 1) {
			return;
		}
		if (type == 2 && newHeight < 1) {
			return;
		}
 
		// 生成新图片
		try {
			BufferedImage ImageNew = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
			int height_i = 0;
			int width_i = 0;
			for (int i = 0; i < images.length; i++) {
				if (type == 1) {
					ImageNew.setRGB(width_i, 0, images[i].getWidth(), newHeight, ImageArrays[i], 0,
							images[i].getWidth());
					width_i += images[i].getWidth();
				} else if (type == 2) {
					ImageNew.setRGB(0, height_i, newWidth, images[i].getHeight(), ImageArrays[i], 0, newWidth);
					height_i += images[i].getHeight();
				}
			}
			// 输出想要的图片
			ImageIO.write(ImageNew, targetFile.split("\\.")[1], new File(targetFile));
 
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
 
	/**
	 * 
	 * @Description:小图片贴到大图片形成一张图(合成)
	 * @author:liuyc
	 * @time:2016年5月27日 下午5:51:20
	 */
	public static final void overlapImage(String bigPath, String smallPath, String outFile) {
		try {
			BufferedImage big = ImageIO.read(new File(bigPath));
			BufferedImage small = ImageIO.read(new File(smallPath));
			Graphics2D g = big.createGraphics();
			int x = (big.getWidth() - small.getWidth()) / 2;
			int y = (big.getHeight() - small.getHeight()) / 2;
			g.drawImage(small, x, y, small.getWidth(), small.getHeight(), null);
			g.dispose();
			ImageIO.write(big, outFile.split("\\.")[1], new File(outFile));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}

